
<Serializable()> Friend Class TableCont

    Private m_description As BikDescription = Nothing
    Private m_name As String = ""

    Private m_firstTable As Table = Nothing
    Private m_next As TableCont = Nothing

    Friend Sub New(ByVal Name As String)
        m_name = Trim(Name)
    End Sub

    Public Property description() As BikDescription
        Get
            Return m_description
        End Get
        Set(ByVal value As BikDescription)
            m_description = value
        End Set
    End Property
    Public Property Name_() As String
        Get
            Return m_name
        End Get
        Set(ByVal value As String)
            m_name = value
        End Set
    End Property
    Friend Function addTable(ByRef map_ As Table) As Boolean
        If map_.Address >= -1 And map_.Name <> vbNullString Then
            If IsNothing(m_firstTable) Then
                m_firstTable = map_
            Else
                m_firstTable.addNode(map_)
            End If
            Return True
        Else
            Return False
        End If
    End Function
    Friend Function addTable(ByRef tables() As Table) As Boolean
        If Not IsNothing(tables) Then
            If tables.Length >= 1 Then
                For Each tab As Table In tables
                    If IsNothing(m_firstTable) Then m_firstTable = tab Else m_firstTable.addNode(tab)
                Next
            End If
        End If
    End Function
    Friend Function addNode(ByRef map_ As TableCont) As Boolean
        If Not map_.Name = vbNullString Then
            If IsNothing(m_next) Then
                m_next = map_
            Else
                m_next.addNode(map_)
            End If
            Return True
        Else
            Return False
        End If
    End Function

    Friend Function length() As Integer
        Dim intReturn As Integer = 0
        If Not IsNothing(m_firstTable) Then
            intReturn = m_firstTable.lengthTotal()
        End If
        Return (intReturn)
    End Function
    Friend Function lengthTotal() As Integer
        Dim intReturn As Integer = 0
        If Not IsNothing(m_firstTable) Then
            intReturn += m_firstTable.lengthTotal
        End If
        If Not IsNothing(m_next) Then
            intReturn += m_next.lengthTotal
        End If
    End Function
    ''' <summary>
    ''' Gets the count of containers in this Link List
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Friend Function count() As Integer
        Dim intReturn As Integer = 1
        If Not IsNothing(m_next) Then
            intReturn += m_next.count
        End If
        Return intReturn
    End Function
    ''' <summary>
    ''' Gets the count of tables under this table container
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Friend Function countTables() As Integer
        Dim intReturn As Integer = 0
        If Not IsNothing(m_firstTable) Then
            intReturn += m_firstTable.count
        End If
        Return intReturn
    End Function
    ''' <summary>
    ''' Gets the Total count of tables from all table containers from initial container onwards
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Friend Function countTotal() As Integer
        Dim intReturn As Integer = 0
        If Not IsNothing(m_firstTable) Then
            intReturn += m_firstTable.count
        End If
        If Not IsNothing(m_next) Then
            intReturn += m_next.countTotal
        End If
        Return intReturn
    End Function
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
    Public Property Name() As String
        Get
            Return m_name
        End Get
        Set(ByVal value As String)
            m_name = value
        End Set
    End Property

    Friend Function getByAddress(ByVal Address_ As Integer) As Table
        Dim f_cursor As Table
        Dim found As Boolean = False
        If Not IsNothing(m_firstTable) Then
            f_cursor = m_firstTable
            If f_cursor.Address = Address_ Then
                found = True
                GoTo endMod
            End If
            While Not found
                If Not IsNothing(f_cursor.getNext) Then
                    f_cursor = f_cursor.getNext
                    If f_cursor.Address = Address_ Then
                        found = True
                        GoTo endMod
                    End If
                Else
                    found = False
                    GoTo endMod
                End If
            End While
        Else
            Return Nothing
        End If
endMod:
        If found Then Return f_cursor Else Return Nothing
    End Function
    Friend Function getByIndex(ByVal Index_ As Integer) As Table
        Dim f_cursor As Table
        Dim found As Boolean = False
        If Not IsNothing(m_firstTable) Then
            f_cursor = m_firstTable
            If Index_ = 0 Then
                Return f_cursor
                found = True
            Else
                For count As Integer = 1 To Index_
                    If Not IsNothing(f_cursor.getNext) Then
                        f_cursor = f_cursor.getNext
                        found = True
                    Else
                        Return Nothing
                        Exit For
                    End If
                Next
            End If
        Else
            Return Nothing
        End If
        If found = False Then Return Nothing Else Return f_cursor
    End Function
    Default Friend ReadOnly Property index(ByVal ind As Integer) As Table
        Get
            Return Me.getByIndex(ind)
        End Get
    End Property
    Friend Function getAsArray() As Table()
        Dim f_count As Integer
        Dim f_mapArray() As Table = Nothing
        If Not IsNothing(m_firstTable) Then
            f_count = m_firstTable.count
            ReDim f_mapArray(0 To f_count - 1)
            For i As Integer = 0 To f_count - 1
                f_mapArray(i) = Me.getByIndex(i)
            Next
        End If
        Return f_mapArray
    End Function
    Friend Function removeByAddress(ByVal Address_ As Integer) As Boolean
        Dim s_first As Table
        Dim s_temp As Table
        Dim found As Boolean = False

        If Not IsNothing(m_firstTable) Then
            If m_firstTable.Address = Address_ Then
                If IsNothing(m_firstTable.getNext) Then
                    m_firstTable = Nothing
                Else
                    s_temp = m_firstTable.getNext
                    m_firstTable = Nothing
                    Me.addTable(s_temp)
                End If
            Else
                ' First node not a match.. now check all other nodes
                s_first = m_firstTable
                Do
                    If s_first.getNext.Address = Address_ Then
                        'next node is a match..
                        If IsNothing(s_first.getNext.getNext) Then
                            'next node has no next.. so just null delete the link
                            s_first.setNext(Nothing)
                            found = True
                            GoTo endSub
                        Else
                            'the next node has a next.. so must swap the links
                            s_temp = s_first.getNext.getNext
                            s_first.setNext(s_temp)
                            found = True
                            GoTo endSub
                        End If
                    End If
                    s_first = s_first.getNext
                Loop Until found Or IsNothing(s_first.getNext)
            End If
        End If
endSub:
        s_first = Nothing
        s_temp = Nothing
        If found Then Return True Else Return False
    End Function
    Friend Function removeByIndex(ByVal Index_ As Integer) As Boolean
        Dim ret As Boolean = False
        Dim f_map As Table = Me.getByIndex(Index_)
        If Not IsNothing(f_map) Then
            Me.removeByAddress(f_map.Address)
            ret = True
        End If
        Return ret
    End Function
    Friend Function getFirstTable() As Table
        Return m_firstTable
    End Function
    Friend Function getNext() As TableCont
        Return m_next
    End Function
    Friend Sub setNext(ByRef TableC As TableCont)
        m_next = TableC
    End Sub
    Friend Sub setFirstTable(ByRef Tab As Table)
        m_firstTable = Tab
    End Sub

End Class
